package com.cq.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.amarsoft.are.lang.StringX;

public class StringUtil
{
    public static void main(String[] args)
    {
        File file = new File("etc/1515462502498.txt");
        try
        {
            System.out.println(readFile(file));
        } catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
    }
    public static String readFile(File file) throws UnsupportedEncodingException
    {
        StringBuffer sb = new StringBuffer();
        //FileReader fr = null;
        BufferedReader br = null;
        try
        {
            //fr = new FileReader(file);
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            String info;
            while ((info = br.readLine()) != null)
            {
                sb.append(info);
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                br.close();
//                fr.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return new String(sb.toString().getBytes("UTF-8"),"UTF-8");
    }

    /**
     * 如果为null转化为""
     * */
    public static String trimNull(Object s)
    {
        if (s == null)
            return "";
        String t = s.toString().trim();
        if (t == null)
        {
            return "";
        }
        else
        {
            return t;
        }
    }

    /**
     * 判空
     * 
     * @return null 如果为空则返回null
     * */
    public static String trim(Object s)
    {
        if (s == null)
            return null;
        else
        {
            String t = s.toString().trim();
            if ("--".equals(t))
                return null;
            if (t.length() == 0)
                return null;
            else
                return t;
        }
    }

    /**
     * 格式化金额成double类型
     * 
     * @return 0.0 如果为空则返回0.0
     * */
    public static Double selectDigit(String s)
    {
        Double digit = 0.0;
        String ss = trim(s);
        if (ss != null)
        {
            String regex = "([1-9][\\d|,]*\\.?\\d*)|(0\\.\\d*[0-9])";
            Pattern pat = Pattern.compile(regex); // 将给定的正则表达式编译到模式中。
            Matcher matcher = pat.matcher(ss);// 创建匹配给定输入与此模式的匹配器。
            if (matcher.find())
            {// 尝试查找与该模式匹配的输入序列的下一个子序列。
                String selected = matcher.group(0);// 捕获组是从 1 开始从左到右的索引。组零表示整个模式，因此表达式 m.group(0) 等效于 m.group()。
                digit = Double.valueOf(selected.replaceAll(",", ""));
            }
            else
                digit = 0.0;
        }

        return digit;
    }

    /**
     * 计算两个日期之间的月份差
     * */
    public static Integer betweenMonths(String dateStr1, String dateStr2) throws ParseException
    {
        if (dateStr1 == null || dateStr2 == null)
            return null;
        else
        {
            String date1 = getDateString(dateStr1).substring(0, 6);// 去掉"\"和空格
            String date2 = getDateString(dateStr2).substring(0, 6);
            int year = Integer.valueOf(date2.substring(0, 4)) - Integer.valueOf(date1.substring(0, 4));
            int month = Integer.valueOf(date2.substring(4, 6)) - Integer.valueOf(date1.substring(4, 6));
            return year * 12 + month;
        }
    }

    /**
     * 格式化日期格式的数据
     * */
    public static String getDateString(String dateString)
    {
        try
        {
            Date date = StringX.parseDate(dateString);
            String dateStr = new SimpleDateFormat("yyyyMMdd").format(date);
            return dateStr.substring(0, 8);
        } catch (Exception e)
        {
            String[] dateList;
            if (dateString.length() > 6)
            {
                return dateString.replaceAll("\\D+", "");
            }
            else if (dateString.contains("."))
            {
                dateList = dateString.split("[.]");
                return dateList[0] + "0" + dateList[1];
            }
            else if (dateString.contains("-"))
            {
                dateList = dateString.split("-");
                return dateList[0] + "0" + dateList[1];
            }
            else if (dateString.contains("/"))
            {
                dateList = dateString.split("/");
                return dateList[0] + "0" + dateList[1];
            }
        }
        return dateString;
    }

    public static Integer betweenMonths(String dateStr1, Date date2) throws ParseException
    {
        if (dateStr1 == null || date2 == null)
            return null;
        else
        {
            String date1 = getDateString(dateStr1).substring(0, 6);
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(date2);
            int year = gc.get(GregorianCalendar.YEAR) - Integer.valueOf(date1.substring(0, 4));
            int month = gc.get(GregorianCalendar.MONTH) + 1 - Integer.valueOf(date1.substring(4, 6));
            return year * 12 + month;
        }
    }

    public static Long betweenDays(String dateStr1, String dateStr2) throws ParseException
    {
        if (dateStr1 == null || dateStr2 == null)
            return null;
        else
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Date date1 = sdf.parse(getDateString(dateStr1).substring(0, 8));
            Date date2 = sdf.parse(getDateString(dateStr2).substring(0, 8));
            return (date2.getTime() - date1.getTime()) / (3600 * 24 * 1000);
        }
    }

    public static Integer betweenDays(String dateStr1, Date date2) throws ParseException
    {
        if (dateStr1 == null || date2 == null)
            return null;
        else
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Date date1 = sdf.parse(getDateString(dateStr1).substring(0, 8));
            return (int) (date2.getTime() - date1.getTime()) / (3600 * 24 * 1000);
        }
    }

    /**
     * 求最大值
     * */
    public static Integer max3I(Integer obj1, Integer obj2, Integer obj3)
    {
        Integer obj = 0;
        if (obj1 != null)
        {
            obj = obj1;
            if (obj2 != null)
                obj = Math.max(obj, obj2);
            if (obj3 != null)
                obj = Math.max(obj, obj3);
        }
        else
        {
            if (obj2 != null)
            {
                obj = obj2;
                if (obj3 != null)
                    obj = Math.max(obj, obj3);
            }
            else
            {
                if (obj3 != null)
                    obj = obj3;
            }
        }
        return obj;
    }

    public static boolean matches(String str, String regExp)
    {
        return str == null ? false : str.matches(regExp);
    }
}
